module dactor.file;

private import core.sys.posix.unistd;
private import core.sys.posix.signal;
private import linux.aio;
private import dactor.eventlet;

class File {
    private int handle = -1;

    public this(int h = -1) {
        handle = h;
    }

    public void close() {
        .close(handle);
        handle = -1;
    }

    public int write(char[] buffer, int timeout=-1) {
        auto ev = new AioEvent!(64)(handle, buffer, Event.WRITE, timeout);
        if (ev.wait() == Event.SUCCESS)
            return ev.aioRet;
        // FIXME: errno not set
        return -1;
    }

    public int read(char[] buffer, int timeout=-1) {
        auto ev = new AioEvent!(64)(handle, buffer, Event.READ, timeout);
        if (ev.wait() == Event.SUCCESS)
            return ev.aioRet;
        // FIXME: errno not set
        return -1;
    }
}
